import { Vector2 } from "../vector";  
import { Engine } from "../engine";  
import { CameraEffect } from "./camera-effect";  
import { lerp } from "../utils";  
  
export class CharacterFocusEffect extends CameraEffect {  
  private startPos: Vector2;  
  private targetOffset: Vector2;  
  private zoomLevel: number;  
  private followIntensity: number;  
  
  constructor(  
    engine: Engine,   
    targetOffset: Vector2 = new Vector2(0, -50),   
    zoomLevel: number = 1.2,  
    followIntensity: number = 0.8  
  ) {  
    super(engine);  
    this.targetOffset = targetOffset;  
    this.zoomLevel = zoomLevel;  
    this.followIntensity = followIntensity;  
  }  
  
  start(duration: number = 3000): void {  
    super.start(duration);  
    this.startPos = this.engine.camera.pos.copy();  
  }  
  
  apply(basePos: Vector2): Vector2 {  
    if (!this.isActive) return basePos;  
  
    const playerPos = this.engine.player.body_.pos;  
    const canvas = this.engine.canvas_;  
      
    // 计算理想的相机位置（角色居中 + 偏移）  
    // const idealPos = new Vector2(  
    //   playerPos.x - canvas.width / 2 + this.targetOffset.x,  
    //   playerPos.y - canvas.height / 2 + this.targetOffset.y  
    // );  
    const idealPos = new Vector2(  
      playerPos.x,  
      playerPos.y
    );  
  
    // 平滑过渡到理想位置  
    const smoothness = 0.05 + (this.progress * 0.15);  
    const focusPos = new Vector2(  
      lerp(basePos.x, idealPos.x, smoothness * this.followIntensity),  
      lerp(basePos.y, idealPos.y, smoothness * this.followIntensity)  
    );  
  
    // 应用缩放效果（通过调整相机位置模拟）  
    if (this.zoomLevel !== 1) {  
      const centerX = canvas.width / 2;  
      const centerY = canvas.height / 2;  
      const zoomFactor = lerp(1, this.zoomLevel, this.progress);  
        
      focusPos.x = (focusPos.x - centerX) * (2 - zoomFactor) + centerX;  
      focusPos.y = (focusPos.y - centerY) * (2 - zoomFactor) + centerY;  
    }  
  
    return focusPos;  
  }  
}